#include "precomp.h"
#include "HUD_Object.h"

HUD_Object::HUD_Object(CL_GraphicContext *gc)
		: _gc(gc)
{
	_image = NULL;
	_font = NULL;

	_x = _y = 0;
}

HUD_Object::~HUD_Object(){
	for(vector<HUD_Object*>::iterator it = _objects.begin(); it != _objects.end(); it++){
		delete *it;
	}

	if(_image != NULL)
		delete _image;

	if(_font != NULL)
		delete _font;
}

	void HUD_Object::set_image(const char* image_dir, CL_Pointf position){
		_image = new CL_Image(*_gc, image_dir);
		_image_position = position;
	}

	void HUD_Object::change_image(const char* image_dir){
		if(_image != NULL){
			delete _image;
			_image = NULL;
		}

		set_image(image_dir, _image_position);
	}

	void HUD_Object::set_font(CL_String text, int font_size, CL_Pointf position, CL_Color color , const wchar_t* font_type){
		_font = new CL_Font(*_gc, font_type, font_size);
		_font_position = position;
		_font_text = text;
		_font_color = color;
	}
	void HUD_Object::change_text(CL_String text){
		_font_text = text;
	}

	void HUD_Object::draw(CL_GraphicContext *gc){
		draw(_x, _y, gc);
	}

	void HUD_Object::draw(CL_Pointf p, CL_GraphicContext *gc){
		draw(p.x, p.y, gc);
	}

	void HUD_Object::draw(float x, float y, CL_GraphicContext *gc){
		if(gc == NULL){
			if(_gc == NULL){
				throw CL_Exception("draw_obj: No CL_GraphicContext");
			}
			gc = _gc;
		}

		for(vector<HUD_Object*>::iterator it = _objects.begin(); it != _objects.end(); it++){
			(*it)->draw( (*it)->get_x() + x,  (*it)->get_y() + y, gc);
		}

		if(_image != NULL){
			_image->draw(*gc, _image_position.x + x, _image_position.y + y);
		}
		if(_font != NULL){
			_font->draw_text(*gc, CL_Pointf(_font_position.x + x, _font_position.y + y), _font_text, CL_Colorf(_font_color));
		}
	}

	void HUD_Object::add(HUD_Object *object){
		_objects.push_back(object);
	}